package com.gm.baseplatform.base.module;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gm.baseplatform.core.utils.ObjectUtil;

public class ModuleManagerImpl implements ModuleManager {

	private ModuleDao moduleDao;
	public void setModuleDao(ModuleDao moduleDao) {
		this.moduleDao = moduleDao;
	}

	public void addModule(Module module) {
		moduleDao.saveModule(module);
	}

	public List<?> loadChildModule(String parent) {
		return moduleDao.loadChildModule(parent);
	}
	
	public List<?> loadChildModule(String parent,int firstResult,final int maxResult) {
		return moduleDao.loadChildModule(parent,firstResult,maxResult);
	}

	public List<?> loadFirstLevelModule() {
		return moduleDao.loadFirstLevelModule();
	}

	public Map<Module, List<?>> buildModuleTreeForPortal() {
		List<?> parents = loadFirstLevelModule();
		if (ObjectUtil.isNull(parents) || parents.isEmpty()) {
			return null;
		}
		Map<Module, List<?>> moduleTree = new HashMap<Module, List<?>>();
		for (int i = 0; i < parents.size(); i++) {
			Module parent = (Module)parents.get(i);
			if (parent == null) {
				continue;
			}
			List<?> childs = loadChildModule(parent.getUnid());
			moduleTree.put(parent, childs);
		}
		return moduleTree;
	}

	public Integer getChildCount(String parent) {
		return moduleDao.getChildCount(parent);
	}

	public Module getModuleByUnid(String unid) {
		return moduleDao.findModuleByUnid(unid);
	}
}
